package com.wondersgroup.hs.mobile.report.support.submitJob;

import com.fr.base.Parameter;
import com.fr.data.ClassSubmitJob;
import com.fr.data.SubmitJob;
import com.fr.design.ExtraDesignClassManager;
import com.fr.design.beans.BasicBeanPane;
import com.fr.design.fun.SubmitProvider;
import com.fr.design.fun.impl.AbstractSubmitProvider;
import com.fr.design.gui.frpane.ObjectProperiesPane;
import com.fr.design.gui.icombobox.UIComboBox;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.itextfield.UITextField;
import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.design.scrollruler.ModLineBorder;
import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.design.write.submit.CustomSubmitJobPane;
import com.fr.general.ComparatorUtils;
import com.fr.general.Inter;
import com.fr.stable.ListMap;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.*;
import java.util.List;

/**
 * Created by zarra on 2016/12/29.
 */
public class NursingDocSubmitPane  extends BasicBeanPane<NursingDocSubmiter> {

    private static final String SubmitClassName = NursingDocTotalSubmitJob.class.getName();

    //private JPanel customCardPane = null;
    private Map<String, BasicBeanPane> customSubmitPanes = null;
    private final Map<String, String> comboItemsMap;

    protected ObjectProperiesPane objectProperiesPane;
    protected UITextField urlTextField;
    protected UITextField templateCodeTextField;


    private NursingDocSubmiter editing;

    private static final String DEFAULT_PANE_TYPE = "submitnormal";

    public NursingDocSubmitPane() {
        this.setLayout(FRGUIPaneFactory.createBorderLayout());
        //customCardPane = FRGUIPaneFactory.createCardLayout_S_Pane();
        customSubmitPanes = new HashMap<>();
        comboItemsMap = new HashMap<>();


        urlTextField = new UITextField(30);
        templateCodeTextField = new UITextField(30);

        objectProperiesPane = new ObjectProperiesPane();
        objectProperiesPane.setBorder(GUICoreUtils.createTitledBorder(Inter.getLocText("Property"), null));



        JPanel headPanel = FRGUIPaneFactory.createY_AXISBoxInnerContainer_L_Pane();

        JPanel urlPane = GUICoreUtils.createFlowPane(new Component[]{new UILabel("服务地址:" ), urlTextField},
                FlowLayout.LEFT, 10);

        JPanel templateCodePane = GUICoreUtils.createFlowPane(new Component[]{new UILabel("模版代码:" ), templateCodeTextField},
                FlowLayout.LEFT, 10);


        headPanel.add(urlPane);
        headPanel.add(templateCodePane);
//        JPanel typePane = GUICoreUtils.createFlowPane(new Component[]{new UILabel(Inter.getLocText(new String[]{"Choose", "Type"}) + ":"), csjConfigComboBox},
//                FlowLayout.LEFT, 10);
        headPanel.setBorder(BorderFactory.createTitledBorder(new ModLineBorder(ModLineBorder.TOP), "文书填报:"));
        this.add(headPanel, BorderLayout.NORTH);

        this.add(objectProperiesPane, BorderLayout.CENTER);
    }

    private void addSubmitPane(SubmitProvider provider) {
        customSubmitPanes.put(provider.keyForSubmit(), provider.appearanceForSubmit());
        comboItemsMap.put(provider.keyForSubmit(), provider.dataForSubmit());
    }

    @Override
    public void populateBean(NursingDocSubmiter ob) {
        editing = ob;
        SubmitJob submitJob = ob.getSubmitJob();

        if (submitJob == null) {
            //csjConfigComboBox.setSelectedItem(comboItemsMap.get(DEFAULT_PANE_TYPE));

            ClassSubmitJob classSubmitJob = emptySubmitJob();

            submitJob = classSubmitJob;

        }
        if (submitJob instanceof ClassSubmitJob){
            ClassSubmitJob classSubmitJob = (ClassSubmitJob) submitJob;
            ListMap listMap = classSubmitJob.getPropertyMap();

            Object objectUrl = listMap.get(NursingDocTotalSubmitJob.KeyServiceURL);
            if (objectUrl!=null){
                urlTextField.setText(objectUrl.toString());
                listMap.remove(NursingDocTotalSubmitJob.KeyServiceURL);
            }

            Object objectTemplateCode = listMap.get(NursingDocTotalSubmitJob.KeyTemplateCode);
            if (objectTemplateCode!=null){
                templateCodeTextField.setText(objectTemplateCode.toString());
                listMap.remove(NursingDocTotalSubmitJob.KeyTemplateCode);
            }



            objectProperiesPane.populateBean(listMap);
        }

//        String pantype = submitJob.getJobType();
//        BasicBeanPane pane = customSubmitPanes.get(pantype);
//        if (pane != null) {
//            //csjConfigComboBox.setSelectedItem(comboItemsMap.get(pantype));
//            pane.populateBean(submitJob);
//        }


    }


    public ListMap emptyListMap(){
        ListMap listMap = new ListMap();

        listMap.put(NursingDocTotalSubmitJob.KeyDocumentID,new Parameter("documentID"));
        listMap.put(NursingDocTotalSubmitJob.KeyPatientID,new Parameter("patientID"));
        listMap.put(NursingDocTotalSubmitJob.KeyNurseID,new Parameter("nurseID"));

        return listMap;
    }

    public ClassSubmitJob emptySubmitJob(){
        ClassSubmitJob classSubmitJob = new ClassSubmitJob(SubmitClassName);
        classSubmitJob.setPropertyMap(emptyListMap());
        return classSubmitJob;
    }

    @Override
    public NursingDocSubmiter updateBean() {
//        for (Map.Entry<String, BasicBeanPane> entry : customSubmitPanes.entrySet()) {
//            BasicBeanPane pane = entry.getValue();
//            if (pane != null && pane.isVisible()) {
//                editing.setSubmitJob((SubmitJob) pane.updateBean());
//            }
//        }
        ClassSubmitJob classSubmitJob = new ClassSubmitJob(SubmitClassName);

        ListMap listMap = this.objectProperiesPane.updateBean();

        String url = urlTextField.getText();
        String templateCode = templateCodeTextField.getText();

        listMap.put(NursingDocTotalSubmitJob.KeyServiceURL,url);
        listMap.put(NursingDocTotalSubmitJob.KeyTemplateCode,templateCode);

        classSubmitJob.setPropertyMap(listMap);
        //checkAddButtonEnable();

        editing.setSubmitJob(classSubmitJob);


        return editing;
    }

    @Override
    protected String title4PopupWindow() {
        return "护理文书";
    }
}
